
*** Bunching Analysis: Robustness Checks / Inference *** 

	**********------------------------------**********
		* Step 1: Baseline Regression 
			* bunching window of 1.0 width
			* outcome: log + 1, ihs, 4 different clusters  
			* estimates store B* 
		* Step 2: Interaction Terms 
			* bunching window of 1.0 width 
			* outcome: log + 1, ihs, 4 different clusters   
			* estimates store I*
		* Step 3: Export tables to .tex format  	 
	**********------------------------------**********

capture log close 
clear all
set more off 

* Set directory
global server "R:\WSV2\TBu_AKe" 
cd "$server" // set working directory 

capture mkdir Bunching_NEW
global store "${server}\Bunching_NEW"

*Define sub directories
global desktop "C:\Users\hy65byfe\Desktop\smerge_0712"
*cd "$desktop" // set working directory 

cd "$store"
log using bunching_3_errors_2703, replace

cd // display in log file 

use bunching_2_collapse_1512 // load data created in bunching_1_bins.do  

** ----------------------------------**
* Prepare for regression  
** ----------------------------------**

   ** reduce file size 
keep if year == 2011 | year == 2014 
drop if ccode == 0 

	** time structure: cut down to two periods: 2011. vs. 2014  
gen postban = 0 if year == 2011 
replace postban = 1 if year == 2014 
codebook eei_bin // 260 values 
tab postban, missing  // no missings. 

   ** outcome transformations   
gen log_units = ln(cell_date_sales)   	// at monthly frequency  

foreach varx of varlist csd ccd {
   gen     log_`varx'=log(1+`varx')
   gen     ihs_`varx'=`varx'+(`varx'^2+1)^0.5
   replace ihs_`varx'=log(ihs_`varx')
	}
 

	** bunching segments : 4 different widths  
	
gen bunch_W05 = 0  
replace bunch_W05 = 1 if eei_bin > 58 & eei_bin < 59

gen bunch_W1 = 0  
replace bunch_W1 = 1 if eei_bin > 57.5 & eei_bin < 59
	
gen bunch_W15 = 0  
replace bunch_W15 = 1 if eei_bin > 57.0 & eei_bin < 59  
	
gen bunch_W2 = 0  
replace bunch_W2 = 1 if eei_bin > 56.5 & eei_bin < 59
	
	** polynomials of EEI, in bins   
gen eei_2 = eei_bin^2
gen eei_3 = eei_bin^3
	 

*******------------------------***
* Bunching regressions *
*******------------------------***

			
		********************************************************
		** Step 3: Bunching in the EEI (Table E-1 Appendix)   **
		********************************************************

			
		** W = 1.0 

foreach varx of varlist log_csd ihs_csd {
		
		
		** Cubic  	
regress `varx' 			i.bunch_W1  		ib3.ccode eei_bin eei_2 eei_3 c.eei_bin#ib3.ccode c.eei_2#ib3.ccode c.eei_3#ib3.ccode i.month     if  postban == 1 & restricted != 1,   vce(cluster ccode)

estimates store B1ccode_`varx'

regress `varx' 			i.bunch_W1  		ib3.ccode eei_bin eei_2 eei_3 c.eei_bin#ib3.ccode c.eei_2#ib3.ccode c.eei_3#ib3.ccode i.month     if  postban == 1 & restricted != 1,   robust 

estimates store B1robust_`varx'


regress `varx' 			i.bunch_W1  		ib3.ccode eei_bin eei_2 eei_3 c.eei_bin#ib3.ccode c.eei_2#ib3.ccode c.eei_3#ib3.ccode i.month     if  postban == 1 & restricted != 1,   vce(cluster eei_bin) 

estimates store B1bin_`varx'


reghdfe `varx' 			i.bunch_W1  		ib3.ccode eei_bin eei_2 eei_3 c.eei_bin#ib3.ccode c.eei_2#ib3.ccode c.eei_3#ib3.ccode     if  postban == 1 & restricted != 1, absorb(month)  vce(cluster ccode eei_bin) 

estimates store B1double_`varx'


}

	*********************
	** Report Results ***
	*********************
		
	** Cubic, Narrow bunching window 

esttab B1*, 	b(%9.3f) se(%9.3f) noconstant stats(r2 N) star(* 0.10 ** .05 *** .01) keep(1.bunch* eei_bin eei_2 eei_3 *.ccode) 		nogap mtitles replace

	
		*****************************************************************
		**  Step 4: Displaced vs. Inframarginal  (Table E-2 Appendix)  **
		*****************************************************************

		
		** W = 1.0 

foreach varx of varlist log_csd ihs_csd {
		
		
		** Cubic  	
regress `varx' 			i.bunch_W1  		i.bunch_W1##i.postban c.eei_bin#i.postban c.eei_2#i.postban c.eei_3#i.postban eei_bin eei_2 eei_3			 			ib3.ccode i.month     if  postban != . & restricted != 1,   vce(cluster ccode)

estimates store I1ccode_`varx'

regress `varx' 			i.bunch_W1  		i.bunch_W1##i.postban c.eei_bin#i.postban c.eei_2#i.postban c.eei_3#i.postban eei_bin eei_2 eei_3			 			ib3.ccode i.month     if  postban != . & restricted != 1,   robust 

estimates store I1robust_`varx'


regress `varx' 			i.bunch_W1##i.postban c.eei_bin#i.postban c.eei_2#i.postban c.eei_3#i.postban eei_bin eei_2 eei_3			 			ib3.ccode i.month     if  postban != . & restricted != 1,   vce(cluster eei_bin) 

estimates store I1bin_`varx'


reghdfe `varx' 			i.bunch_W1##i.postban c.eei_bin#i.postban c.eei_2#i.postban c.eei_3#i.postban eei_bin eei_2 eei_3			 			ib3.ccode		     if  postban != . & restricted != 1,  absorb(month) vce(cluster ccode eei_bin) 

estimates store I1double_`varx'


}
		
	** Cubic, Narrow bunching window 

esttab I1*, 	b(%9.3f) se(%9.3f) noconstant stats(r2 N) star(* 0.10 ** .05 *** .01) keep(1.bunch* eei_bin eei_2 eei_3 *.ccode) 		nogap mtitles replace		
		
	
		*****************************
		**  Step 5: Export to Tex  **
		*****************************
	
	
#delimit ; 	
esttab B1bin_log_csd B1bin_ihs_csd B1robust_log_csd B1robust_ihs_csd B1double_log_csd B1double_ihs_csd  using B1_errors.tex, b(%9.3f) se(%9.3f) stats(r2 N) star(* 0.10 ** .05 *** .01) keep(1.bunch* eei_bin eei_2 eei_3 *.ccode) order(1.bunch* eei_bin eei_2 eei_3 *.ccode) nogap mtitles title(Error Structure Table table\label{B1_cube}) replace; #delimit cr 		
		
#delimit ; 	
esttab I1bin_log_csd I1bin_ihs_csd I1robust_log_csd I1robust_ihs_csd I1double_log_csd I1double_ihs_csd  using I1_errors.tex, b(%9.3f) se(%9.3f) stats(r2 N) star(* 0.10 ** .05 *** .01) keep(1.bunch* eei_bin eei_2 eei_3 *.ccode) order(1.bunch* eei_bin eei_2 eei_3 *.ccode) nogap mtitles title(Error Structure Table table\label{I1_cube}) replace; #delimit cr 	

log close

clear 

exit 	
		